package com.taxsystem.gaozhuanglin.invoicebusiness;

import java.util.concurrent.ExecutionException;

import org.json.JSONObject;

import com.main.taxsystem.R;
import com.taxsystem.tool.HttpUtil;



import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class VerifyActivity extends Activity {
	
	EditText word;
	EditText num;
	EditText money;
	
	Button next;
	Button clear;
	
	private String m1;
	private String m2;
	private String m3;
	private String m4;
	private String m5;
	private String m6;
	private String m7;
	private String m8;
	private String m9;
	
	private Handler handler = null;
	
	private LocalBroadcastManager localBroadcastManager = null;
	
	private final static String url = "";
	private final static String ACTION_VERIFY = "VerifyActivity.HTTP_ACTION";
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.verify_layout);
		
		word = (EditText)findViewById(R.id.invoice_word);
		num = (EditText)findViewById(R.id.invoice_num);
		money = (EditText)findViewById(R.id.invoice_money);
		
		next = (Button)findViewById(R.id.nextBtn);
		clear = (Button)findViewById(R.id.clearBtn);
		
		//注册Broadcast Receive
		/*localBroadcastManager = LocalBroadcastManager.getInstance(VerifyActivity.this);
		localBroadcastManager.registerReceiver(new BroadcastReceiver(){
			@Override
			public void onReceive(Context context, Intent intent) {
				//获取JSON，并进行解析
			}
		}, new IntentFilter(ACTION_VERIFY));
		
		//new MyThread().start();*/
		
		handler = new Handler()
		{
			public void handleMessage(Message msg)
			{
				super.handleMessage(msg);
				if(msg.what==0x234)
				{
					Intent intent1 = new Intent(VerifyActivity.this, VerifyInformationActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("invCode", m1);
					bundle.putString("invNo", m2);
					bundle.putString("kphymc", m3);
					bundle.putString("fkfmc", m4);
					bundle.putString("skfmc", m5);
					bundle.putString("skfzjhm", m6);
					bundle.putString("invTime", m7);
					bundle.putString("amount", m8);
					bundle.putString("mobile", m9);
					intent1.putExtras(bundle);
					startActivity(intent1);
				}
			}
		};
		
		next.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/*if(word.getText().toString().equals("") || num.getText().toString().equals("") || money.getText().toString().equals(""))
				{
					Toast toast = new Toast(VerifyActivity.this);
					toast.setGravity(Gravity.BOTTOM, 0, 0);
					toast.setText("请输入完整信息!");
					toast.setDuration(Toast.LENGTH_SHORT);
					toast.show();
				}
				else
				{
					toGetRequest();
				}*/
				Intent intent1 = new Intent(VerifyActivity.this, VerifyInformationActivity.class);
				startActivity(intent1);
			}		
		});
		
		//清空内容
		clear.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				word.setText("");
				num.setText("");
				money.setText("");
			}
			
		});
	}
	
	/*private class MyThread extends Thread{

		@Override
		public void run() {
			//TODO:add your code here
			Intent intent1 = new Intent(ACTION_VERIFY);
			try {
				String string1 = word.getText().toString();
				String string2 = num.getText().toString();
				String string3 = money.getText().toString();
				
				String result = HttpUtil.getRequest(url);
				intent1.putExtra("invoice_information", result);
				localBroadcastManager.sendBroadcast(intent1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		
	}*/
	
	protected void toGetRequest()
	{
		new Thread(new Runnable()
		{

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message m = new Message();
				m.what = 0x234;
				try {
					String result = HttpUtil.getRequest(url);
					JSONObject json = new JSONObject(result);
					m1 = json.getString("invCode");
					m2 = json.getString("invNo");
					m3 = json.getString("kphymc");
					m4 = json.getString("fkfmc");
					m5 = json.getString("skfmc");
					m6 = json.getString("skfzjhm");
					m7 = json.getString("invTime");
					m8 = json.getString("amount");
					m9 = json.getString("mobile");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				handler.sendMessage(m);
			}
			
		}).start();
	}
}